package org.jiajie.designpatterndemo.chain.middleware;

/**
 * ConcreteHandler.
 * Checks whether there are too many failed login requests.
 * 检查请求数量限制
 *
 * @author jie
 */
public class ThrottlingMiddleware extends Middleware{
    private int requestPerMinute;
    private int request;
    private long currentTime;

    public ThrottlingMiddleware(int requestPerMinute) {
        this.requestPerMinute = requestPerMinute;
        this.currentTime = System.currentTimeMillis();
    }

    /**
     * Plead, not that checkNext() call can be inserted both in the beginning of this method and in the end.
     *
     * This gives much more flexibility than a simple loop over all middleware objects.
     * For instance, an element of a chain can change the order of checks by running its checks after other checks.
     * @param email email
     * @param password  pwd
     * @return boolean
     */
    @Override
    public boolean check(String email, String password) {
        if (System.currentTimeMillis() > currentTime + 60_000){
            request = 0;
            currentTime = System.currentTimeMillis();
        }

        request++;

        if (request > requestPerMinute) {
            System.out.println("Request limit exceeded!");
            Thread.currentThread().stop();
        }
        return checkNext(email, password);
    }
}
